﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.UserManagement;
using System.Configuration;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recRegistrationform : BasePage
    {
        private int id = -1;
        private void Disabled()
        {
            ddlSector.Enabled = false;
            ddlSubSector.Enabled = false;
            ddlSubSubSector.Enabled = false;
            txtNilaiEmisi.Enabled = false;
            ddlJenisEmisi.Enabled = false;
            
        }
        private void DisabledIfDebtorOrRegistrationAfter()
        {
            txtNPWP.Enabled = false;
            txtDebtorName.Enabled = false;
            txtAlamat1.Enabled = false;
            txtAlamat2.Enabled = false;
            txtAlamat3.Enabled = false;
            txtKodePos.Enabled = false;
            txtWebsite.Enabled = false;
            txtTelpon.Enabled = false;
            txtFax.Enabled = false;
            txtEmail.Enabled = false;            
            checkedPerorangan.Enabled = false;
            //ddlWilayah.Items.Clear();
            //ddlWilayah.Items.Insert(0, new ListItem("-- pilih propinsi  --", string.Empty));
        }
        protected override void OnLoad(EventArgs e)
        {
       
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recRegionRepo recRegionDal = new recRegionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recCityRepo recCityDal = new recCityRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recEmisionTypeRepo recEmisionTypeDal = new recEmisionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recRegistrationRepo recRegistratioDal = new recRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recRegistrationEmisionTypeRepo recRegistrationEmisionTypeDal = new recRegistrationEmisionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                //populate dropdownlist Type Registrasi
                List<recRegistrationEmisionType> registrationEmisionTypes = recRegistrationEmisionTypeDal.GetList();
                ddlRegistrationEmisionType.Items.Clear();
                registrationEmisionTypes.ForEach(delegate(recRegistrationEmisionType registrationEmisionType)
                {
                    ddlRegistrationEmisionType.Items.Add(new ListItem(registrationEmisionType.RegistrationEmisionTypeName, registrationEmisionType.RegistrationEmisionTypeID.ToString()));
                });
                ddlRegistrationEmisionType.Items.Insert(0, new ListItem("-- pilih jenis registrasi emisi --", string.Empty));

                //populate dropdownlist sector
                List<recSector> sectors = recSectorDal.GetList();
                ddlSector.Items.Clear();
                sectors.ForEach(delegate(recSector sector)
                {
                    ddlSector.Items.Add(new ListItem(sector.SectorCode + "-" + sector.SectorName, sector.SectorID.ToString()));
                });
                ddlSector.Items.Insert(0, new ListItem("-- pilih sector --", string.Empty));

                //populate dropdownlist Sub sector
                ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sector --", string.Empty));

                //populate dropdownlist Sub sector
                ddlSubSubSector.Items.Insert(0, new ListItem("-- pilih sub sub sector --", string.Empty));

                //populate dropdownlist Region
                List<recRegion> regions = recRegionDal.GetList();
                ddlWilayah.Items.Clear();
                regions.ForEach(delegate(recRegion region)
                {
                    ddlWilayah.Items.Add(new ListItem(region.RegionCode + " - " + region.RegionName, region.RegionID.ToString()));
                });
                ddlWilayah.Items.Insert(0, new ListItem("-- pilih propinsi  --", string.Empty));

                //populate dropdownlist kota
                ddlKota.Items.Insert(0, new ListItem("-- pilih kota  --", string.Empty));

                if (id > 0) // Edit an existing item
                {
                    recRegistrationRepo recRegistrationDal = new recRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recRegistration recRegistration = recRegistrationDal.GetByIdEditItem(id);
                    if (recRegistration != null)
                    {
                        //populate dropdownlist sub sector
                        List<recSubSector> subSectors = recSubSectorDal.GetList();
                        ddlSubSector.Items.Clear();
                        subSectors.ForEach(delegate(recSubSector subsector)
                        {
                            ddlSubSector.Items.Add(new ListItem(subsector.SubSectorCode + " - " + subsector.SubSectorName, subsector.SubSectorID.ToString()));
                        });
                        ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sector --", string.Empty));

                        //populate dropdownlist sub sector
                        List<recSubSubSector> subsubSectors = recSubSubSectorDal.GetList();
                        ddlSubSubSector.Items.Clear();
                        subsubSectors.ForEach(delegate(recSubSubSector subsubSector)
                        {
                            ddlSubSubSector.Items.Add(new ListItem(subsubSector.SubSubSectorCode + " - " + subsubSector.SubSubSectorName, subsubSector.SubSubSectorID.ToString()));
                        });
                        ddlSubSubSector.Items.Insert(0, new ListItem("-- pilih sub sub sector --", string.Empty));

                        //populate dropdownlist Jenis Emisi
                        List<recEmisionType> emisionTypes = recEmisionTypeDal.GetList();
                        ddlJenisEmisi.Items.Clear();
                        emisionTypes.ForEach(delegate(recEmisionType emisionType)
                        {
                            ddlJenisEmisi.Items.Add(new ListItem(emisionType.EmisionTypeName, emisionType.EmisionTypeID.ToString()));
                        });
                        ddlJenisEmisi.Items.Insert(0, new ListItem("-- pilih Jenis Penawaran Umum  --", string.Empty));

                        //populate dropdownlist City
                        List<recCity> citys = recCityDal.GetList();
                        ddlKota.Items.Clear();
                        citys.ForEach(delegate(recCity city)
                        {
                            ddlKota.Items.Add(new ListItem(city.CityCode + " - " + city.CityName, city.CityID.ToString()));
                        });
                        ddlKota.Items.Insert(0, new ListItem("-- pilih kota  --", string.Empty));

                        hfID.Value = id.ToString();
                        hfIsActive.Value = recRegistration.IsActive.ToString();
                        txtDebtorName.Text = recRegistration.PreDebtorName;
                        txtAlamat1.Text = recRegistration.Address1;
                        txtAlamat2.Text = recRegistration.Address2;
                        txtAlamat3.Text = recRegistration.Address3;
                        txtKodePos.Text = recRegistration.ZipCode;
                        txtTelpon.Text = recRegistration.Phone;
                        txtFax.Text = recRegistration.Fax;
                        txtWebsite.Text = recRegistration.Website;
                        txtEmail.Text = recRegistration.Email;
                        txtNPWP.Text = recRegistration.NPWP;
                        ddlSector.Items.FindByValue(recRegistration.recSectorRegistration.SectorID.ToString()).Selected = true;
                        ddlSubSector.Items.FindByValue(recRegistration.recSectorRegistration.SubSectorID.ToString()).Selected = true;
                        ddlSubSubSector.Items.FindByValue(recRegistration.recSectorRegistration.SubSubSectorID.ToString()).Selected = true;
                        if (recRegistration.RegionID != null)
                            ddlWilayah.Items.FindByValue(recRegistration.RegionID.ToString()).Selected = true;
                        if (recRegistration.CityID != null)
                            ddlKota.Items.FindByValue(recRegistration.CityID.ToString()).Selected = true;
                        txtNilaiEmisi.Text = recRegistration.recSectorRegistration.EmisionValue.ToString();
                        if (recRegistration.recSectorRegistration.EmisionTypeID != null)
                            ddlJenisEmisi.Items.FindByValue(recRegistration.recSectorRegistration.EmisionTypeID.ToString()).Selected = true;
                        txtUserId.Text = recRegistration.recUser.UserCode;
                        txtPasswordRegistration.Text = recRegistration.recUser.Password;
                        txtConfirmPassword.Text = recRegistration.recUser.Password;
                        Disabled();

                    }
                }
                if (base.DebtorID.HasValue)
                {
                    hfDebtorID.Value = base.DebtorID.ToString();
                    recDebtor debtor = recDebtorDal.GetById(base.DebtorID.Value);
                    if (debtor != null)
                    {
                        DisabledIfDebtorOrRegistrationAfter();
                        txtDebtorName.Text = debtor.DebtorName;
                        txtNPWP.Text = debtor.NPWP;
                        hfNPWP.Value = debtor.NPWP;
                        txtAlamat1.Text = debtor.Address1;
                        txtAlamat2.Text = debtor.Address2;
                        txtAlamat3.Text = debtor.Address3;
                        txtTelpon.Text = debtor.Phone;
                        txtFax.Text = debtor.Fax;
                        txtKodePos.Text = debtor.ZipCode;
                        txtWebsite.Text = debtor.Website;
                        txtEmail.Text = debtor.Email;
                        if (debtor.IsIndividual == true)
                            checkedPerorangan.Checked = true;
                        if (debtor.RegionID != null)
                            ddlWilayah.Items.FindByValue(debtor.RegionID.ToString()).Selected = true;
                    }
                }
                else 
                {
                    if (base.RegistrationID != 0)
                    {
                        hfDebtorID.Value = base.RegistrationID.ToString();
                        recRegistration recRegistration = new recRegistration();
                        recRegistration = recRegistratioDal.GetById(base.RegistrationID);
                        if (recRegistration != null)
                        {
                            DisabledIfDebtorOrRegistrationAfter();
                            txtDebtorName.Text = recRegistration.PreDebtorName;
                            txtNPWP.Text = recRegistration.NPWP;
                            hfNPWP.Value = recRegistration.NPWP;
                            txtAlamat1.Text = recRegistration.Address1;
                            txtAlamat2.Text = recRegistration.Address2;
                            txtAlamat3.Text = recRegistration.Address3;
                            txtTelpon.Text = recRegistration.Phone;
                            txtFax.Text = recRegistration.Fax;
                            txtKodePos.Text = recRegistration.ZipCode;
                            txtWebsite.Text = recRegistration.Website;
                            txtEmail.Text = recRegistration.Email;
                            if (recRegistration.IsIndividual == true)
                                checkedPerorangan.Checked = true;
                            if (recRegistration.RegionID != null)
                                ddlWilayah.Items.FindByValue(recRegistration.RegionID.ToString()).Selected = true;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(base.SubSectorName))
                {
                    hfSubSectorName.Value = base.SubSectorName.ToLower();
                    recUserRepo recUserDal = new recUserRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recRegistrationRepo recRegistrationDal = new recRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recUser user = recUserDal.GetByUserCode(base.UserLoggedIn);
                    if (user != null)
                    {
                        txtUserId.Text = user.UserCode;
                        txtPasswordRegistration.Text = user.Password;
                        txtConfirmPassword.Text = user.Password;
                    }
                    recRegistration recRegistration = recRegistrationDal.GetByUserID(base.UserID);
                    if (recRegistration != null)
                    {
                        //populate dropdownlist sub sector
                        List<recSubSector> subsectors = recSubSectorDal.GetList();
                        ddlSubSector.Items.Clear();
                        subsectors.ForEach(delegate(recSubSector subsector)
                        {
                            ddlSubSector.Items.Add(new ListItem(subsector.SubSectorCode + "-" + subsector.SubSectorName, subsector.SubSectorID.ToString()));
                        });
                        ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sector --", string.Empty));

                        //populate dropdownlist sub sub sector
                        List<recSubSubSector> subsubsectors = recSubSubSectorDal.GetList();
                        ddlSubSubSector.Items.Clear();
                        subsubsectors.ForEach(delegate(recSubSubSector subsubsector)
                        {
                            ddlSubSubSector.Items.Add(new ListItem(subsubsector.SubSubSectorCode + "-" + subsubsector.SubSubSectorName, subsubsector.SubSubSectorID.ToString()));
                        });
                        ddlSubSubSector.Items.Insert(0, new ListItem("-- pilih sub sub sector --", string.Empty));

                        hfSectorID.Value = recRegistration.recSector.SectorID.ToString();
                        hfSectorCode.Value = recRegistration.recSector.SectorCode;
                        hfSectorName.Value = recRegistration.recSector.SectorName;

                        hfSubSectorID.Value = recRegistration.recSubSector.SubSectorID.ToString();
                        hfSubSectorCode.Value = recRegistration.recSubSector.SubSectorCode;
                        hfSubSectorNames.Value = recRegistration.recSubSector.SubSectorName;

                        hfSubSubSectorID.Value = recRegistration.recSubSubSector.SubSubSectorID.ToString();
                        hfSubSubSectorCode.Value = recRegistration.recSubSubSector.SubSubSectorCode;
                        hfSubSubSectorName.Value = recRegistration.recSubSubSector.SubSubSectorName;

                    }
                    fsUserLogin.Visible = false;
                    trNPWP.Visible = false;
                    trNamaPerusahaan.Visible = false;
                    trJenisUsaha.Visible = false;
                    trAlamat2.Visible = false;
                    trAlamat3.Visible = false;
                    trWebsite.Visible = false;
                    trEmail.Visible = false;
                    trKodePos.Visible = false;
                    tdddlSubSector.Visible = false;
                    tdddlSubSubSector.Visible = false;
                    tdAlamat1.Visible = false;
                    tdtxtAlamat1.Visible = false;
                    tdSeparatorAlamat1.Visible = false;
                    tdddlSubSectorLabel.Visible = false;
                    tdddlSubSectorSeparator.Visible = false;
                    tdddlSubSubSectorLabel.Visible = false;
                    tdddlSubSubSectorSeparator.Visible = false;
                }
            }
        }
    }
}